package com.gm.commonlib.views.popupwindow;
import com.gm.commonlib.R;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
 * 运单支付运费弹窗
 *
 * @author ：hck
 * @date：2017/6/19
 */

public class Popupwindow2 {
    public static PopupWindow mPopupWindow;


    public static PopupWindow showPopView(Activity activity,View pView, final View mainView) {
        if (mPopupWindow != null) {
            hiddenPop();
        }
        mPopupWindow = getPopupWindow(activity);
        mPopupWindow.setContentView(mainView);
        mPopupWindow.setOutsideTouchable(true);
       mPopupWindow.showAsDropDown(pView);
        return mPopupWindow;

    }


    public static PopupWindow showTopPopView(Activity activity,View pView, final View mainView) {
        if (mPopupWindow != null) {
            hiddenPop();
        }
        mPopupWindow = getPopupWindow(activity);
        mPopupWindow.setContentView(mainView);
        mPopupWindow.setOutsideTouchable(true);
        /** 获取父控件的位置 */
        int[] location = new int[2];
        pView.getLocationOnScreen(location);
        /** 这个很重要 ,获取弹窗的长宽度 */
        mainView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = mainView.getMeasuredWidth();
        int popupHeight = mainView.getMeasuredHeight();
/** 显示位置 */
        mPopupWindow.showAtLocation(pView, Gravity.NO_GRAVITY, (location[0] +pView.getWidth() / 2) - popupWidth / 2, location[1]
               +popupHeight );
        mPopupWindow.update();

        return mPopupWindow;

    }



    public static PopupWindow getPopupWindow(final Activity activity) {
        mPopupWindow = new PopupWindow();
        mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setTouchable(true);
      //  mPopupWindow.setAnimationStyle(R.style.showPopupAnimation);
        WindowManager.LayoutParams params = activity.getWindow().getAttributes();
        params.alpha = 0.8f;
        activity.getWindow().setAttributes(params);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
              resetWindowAlpha(activity);
            }
        });

        return mPopupWindow;
    }


    public static void hiddenPop() {
        try {
            if (mPopupWindow != null) {
                mPopupWindow.dismiss();
                mPopupWindow = null;
            }
        } catch (Exception e) {

        }

    }

    public static void hiddenPop(Activity activity) {
        resetWindowAlpha(activity);
        hiddenPop();
    }

    private  static void resetWindowAlpha(Activity activity) {
        if (activity!=null && !activity.isFinishing()){
            WindowManager.LayoutParams params = activity.getWindow().getAttributes();
            params.alpha = 1f;
            activity.getWindow().setAttributes(params);

        }

    }
}
